﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter10
{
    using System.IO;

    partial class Program
    {
        internal static void Begin(Action action, bool active = false)
        {
            if (active)
            {
                Console.WriteLine("开始演示：");
                action();
                Console.WriteLine("演示完毕：");
                Console.ReadLine();
            }
        }

        internal static void C01()
        {
            {
                Employee e = new Employee() { Name = "Jeff", Age = 45 };
            }
            {
                Employee e = new Employee();
                e.Name = "Jeff";
                e.Age = 45;
            }
            {
                string s = new Employee() { Name = "Jeff", Age = 45 }.ToString().ToUpper();
            }
            {
                string s = new Employee { Name = "Jeff", Age = 45 }.ToString().ToUpper();
            }
            {
                M();
            }
            {
                var table = new Dictionary<string, int> { { "Jeffret", 1 }, { "Kristin", 2 }, { "Aidan", 3 }, { "Grant", 4 } };
            }
            {
                var o1 = new { Name = "Jeff", Year = 1964 };
                Console.WriteLine("Name={0}, Year={1}", o1.Name, o1.Year);
                string Name = "Grant";
                DateTime dt = DateTime.Now;
                var o2 = new { Name, dt.Year };
                Console.WriteLine("Objects are equal: " + o1.Equals(o2));
                o1 = o2;
                var people = new[] { new { Name = "Kristin", Year = 1970 }, new { Name = "Aidan", Year = 2003 }, new { Name = "Grant", Year = 2008 } };
                foreach (var person in people)
                {
                    Console.WriteLine("Name={0}, Yaer={1}", person.Name, person.Year);
                }
            }
            {
                string myCookies = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
                var query =
                    from pathname in Directory.GetFiles(myCookies)
                    let LastWriteTime = File.GetLastWriteTime(pathname)
                    where LastWriteTime > (DateTime.Now - TimeSpan.FromDays(7))
                    orderby LastWriteTime
                    select new { Path = pathname, LastWriteTime };
                foreach (var file in query)
                {
                    Console.WriteLine("LastWriteTiem={0}, Path={1}", file.LastWriteTime, file.Path);
                }
            }
        }

        private class Employee
        {
            private string name;
            private int age;

            internal string Name
            {
                get { return name; }
                set { name = value; }
            }

            internal int Age
            {
                get { return age; }
                set
                {
                    if (value < 0 || value > 100)
                        throw new ArgumentOutOfRangeException("value", value.ToString());
                    age = value;
                }
            }
        }

        private static void MethodWithOutParam(out string n)
        {
            n = null;
        }

        private sealed class Classroom
        {
            private List<string> students = new List<string>();
            internal List<string> Students { get { return students; } }
            internal Classroom() { }
        }

        private static void M()
        {
            Classroom classroom = new Classroom { Students = { "Jeff", "Kristin", "Aidan", "Grant" } };
            foreach (var student in classroom.Students)
            {
                Console.WriteLine(student);
            }
        }

        internal static void C02()
        {
            BitArray ba = new BitArray(14);
            for (int i = 0; i < 14; i++)
            {
                ba[i] = (i % 2 == 0);
            }
            for (int i = 0; i < 14; i++)
            {
                Console.WriteLine("Bit " + i + " is " + (ba[i] ? "On" : "Off"));
            }
        }

        private sealed class BitArray
        {
            private byte[] byteArray;
            private int numBits;

            internal BitArray(int numBits)
            {
                if (numBits <= 0)
                    throw new ArgumentOutOfRangeException("numBits must be > 0");
                this.numBits = numBits;
                byteArray = new byte[(numBits + 7) / 8];
            }

            internal bool this[int bitPos]
            {
                get
                {
                    if ((bitPos < 0) || (bitPos >= numBits))
                        throw new ArgumentOutOfRangeException("bitPos");
                    return (byteArray[bitPos / 8] & (1 << (bitPos % 8))) != 0;
                }
                set
                {
                    if ((bitPos < 0 || (bitPos >= numBits)))
                        throw new ArgumentOutOfRangeException("bitPos", bitPos.ToString());
                    if (value)
                    {
                        byteArray[bitPos / 8] = (byte)(byteArray[bitPos / 8] | (1 << (bitPos % 8)));
                    }
                    else
                    {
                        byteArray[bitPos / 8] = (byte)(byteArray[bitPos / 8] & (1 << (bitPos % 8)));
                    }
                }
            }
        }

        private class SmoeType
        {
            private string name;

            internal string Name
            {
                get { return name; }
                set { name = value; }
            }
        }
    }
}
